<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>const.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_string_const.js.html">const.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2013 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>goog.provide(&#39;goog.string.Const&#39;);
<a name="line16"></a>
<a name="line17"></a>goog.require(&#39;goog.asserts&#39;);
<a name="line18"></a>goog.require(&#39;goog.string.TypedString&#39;);
<a name="line19"></a>
<a name="line20"></a>
<a name="line21"></a>
<a name="line22"></a>/**
<a name="line23"></a> * Wrapper for compile-time-constant strings.
<a name="line24"></a> *
<a name="line25"></a> * Const is a wrapper for strings that can only be created from program
<a name="line26"></a> * constants (i.e., string literals).  This property relies on a custom Closure
<a name="line27"></a> * compiler check that {@code goog.string.Const.from} is only invoked on
<a name="line28"></a> * compile-time-constant expressions.
<a name="line29"></a> *
<a name="line30"></a> * Const is useful in APIs whose correct and secure use requires that certain
<a name="line31"></a> * arguments are not attacker controlled: Compile-time constants are inherently
<a name="line32"></a> * under the control of the application and not under control of external
<a name="line33"></a> * attackers, and hence are safe to use in such contexts.
<a name="line34"></a> *
<a name="line35"></a> * Instances of this type must be created via its factory method
<a name="line36"></a> * {@code goog.string.Const.from} and not by invoking its constructor.  The
<a name="line37"></a> * constructor intentionally takes no parameters and the type is immutable;
<a name="line38"></a> * hence only a default instance corresponding to the empty string can be
<a name="line39"></a> * obtained via constructor invocation.
<a name="line40"></a> *
<a name="line41"></a> * @see goog.string.Const#from
<a name="line42"></a> * @constructor
<a name="line43"></a> * @final
<a name="line44"></a> * @struct
<a name="line45"></a> * @implements {goog.string.TypedString}
<a name="line46"></a> */
<a name="line47"></a>goog.string.Const = function() {
<a name="line48"></a>  /**
<a name="line49"></a>   * The wrapped value of this Const object.  The field has a purposely ugly
<a name="line50"></a>   * name to make (non-compiled) code that attempts to directly access this
<a name="line51"></a>   * field stand out.
<a name="line52"></a>   * @private {string}
<a name="line53"></a>   */
<a name="line54"></a>  this.stringConstValueWithSecurityContract__googStringSecurityPrivate_ = &#39;&#39;;
<a name="line55"></a>
<a name="line56"></a>  /**
<a name="line57"></a>   * A type marker used to implement additional run-time type checking.
<a name="line58"></a>   * @see goog.string.Const#unwrap
<a name="line59"></a>   * @const
<a name="line60"></a>   * @private
<a name="line61"></a>   */
<a name="line62"></a>  this.STRING_CONST_TYPE_MARKER__GOOG_STRING_SECURITY_PRIVATE_ =
<a name="line63"></a>      goog.string.Const.TYPE_MARKER_;
<a name="line64"></a>};
<a name="line65"></a>
<a name="line66"></a>
<a name="line67"></a>/**
<a name="line68"></a> * @override
<a name="line69"></a> * @const
<a name="line70"></a> */
<a name="line71"></a>goog.string.Const.prototype.implementsGoogStringTypedString = true;
<a name="line72"></a>
<a name="line73"></a>
<a name="line74"></a>/**
<a name="line75"></a> * Returns this Const&#39;s value a string.
<a name="line76"></a> *
<a name="line77"></a> * IMPORTANT: In code where it is security-relevant that an object&#39;s type is
<a name="line78"></a> * indeed {@code goog.string.Const}, use {@code goog.string.Const.unwrap}
<a name="line79"></a> * instead of this method.
<a name="line80"></a> *
<a name="line81"></a> * @see goog.string.Const#unwrap
<a name="line82"></a> * @override
<a name="line83"></a> */
<a name="line84"></a>goog.string.Const.prototype.getTypedStringValue = function() {
<a name="line85"></a>  return this.stringConstValueWithSecurityContract__googStringSecurityPrivate_;
<a name="line86"></a>};
<a name="line87"></a>
<a name="line88"></a>
<a name="line89"></a>/**
<a name="line90"></a> * Returns a debug-string representation of this value.
<a name="line91"></a> *
<a name="line92"></a> * To obtain the actual string value wrapped inside an object of this type,
<a name="line93"></a> * use {@code goog.string.Const.unwrap}.
<a name="line94"></a> *
<a name="line95"></a> * @see goog.string.Const#unwrap
<a name="line96"></a> * @override
<a name="line97"></a> */
<a name="line98"></a>goog.string.Const.prototype.toString = function() {
<a name="line99"></a>  return &#39;Const{&#39; +
<a name="line100"></a>         this.stringConstValueWithSecurityContract__googStringSecurityPrivate_ +
<a name="line101"></a>         &#39;}&#39;;
<a name="line102"></a>};
<a name="line103"></a>
<a name="line104"></a>
<a name="line105"></a>/**
<a name="line106"></a> * Performs a runtime check that the provided object is indeed an instance
<a name="line107"></a> * of {@code goog.string.Const}, and returns its value.
<a name="line108"></a> * @param {!goog.string.Const} stringConst The object to extract from.
<a name="line109"></a> * @return {string} The Const object&#39;s contained string, unless the run-time
<a name="line110"></a> *     type check fails. In that case, {@code unwrap} returns an innocuous
<a name="line111"></a> *     string, or, if assertions are enabled, throws
<a name="line112"></a> *     {@code goog.asserts.AssertionError}.
<a name="line113"></a> */
<a name="line114"></a>goog.string.Const.unwrap = function(stringConst) {
<a name="line115"></a>  // Perform additional run-time type-checking to ensure that stringConst is
<a name="line116"></a>  // indeed an instance of the expected type.  This provides some additional
<a name="line117"></a>  // protection against security bugs due to application code that disables type
<a name="line118"></a>  // checks.
<a name="line119"></a>  if (stringConst instanceof goog.string.Const &amp;&amp;
<a name="line120"></a>      stringConst.constructor === goog.string.Const &amp;&amp;
<a name="line121"></a>      stringConst.STRING_CONST_TYPE_MARKER__GOOG_STRING_SECURITY_PRIVATE_ ===
<a name="line122"></a>          goog.string.Const.TYPE_MARKER_) {
<a name="line123"></a>    return stringConst.
<a name="line124"></a>        stringConstValueWithSecurityContract__googStringSecurityPrivate_;
<a name="line125"></a>  } else {
<a name="line126"></a>    goog.asserts.fail(&#39;expected object of type Const, got \&#39;&#39; +
<a name="line127"></a>                      stringConst + &#39;\&#39;&#39;);
<a name="line128"></a>    return &#39;type_error:Const&#39;;
<a name="line129"></a>  }
<a name="line130"></a>};
<a name="line131"></a>
<a name="line132"></a>
<a name="line133"></a>/**
<a name="line134"></a> * Creates a Const object from a compile-time constant string.
<a name="line135"></a> *
<a name="line136"></a> * It is illegal to invoke this function on an expression whose
<a name="line137"></a> * compile-time-contant value cannot be determined by the Closure compiler.
<a name="line138"></a> *
<a name="line139"></a> * Correct invocations include,
<a name="line140"></a> * &lt;pre&gt;
<a name="line141"></a> *   var s = goog.string.Const.from(&#39;hello&#39;);
<a name="line142"></a> *   var t = goog.string.Const.from(&#39;hello&#39; + &#39;world&#39;);
<a name="line143"></a> * &lt;/pre&gt;
<a name="line144"></a> *
<a name="line145"></a> * In contrast, the following are illegal:
<a name="line146"></a> * &lt;pre&gt;
<a name="line147"></a> *   var s = goog.string.Const.from(getHello());
<a name="line148"></a> *   var t = goog.string.Const.from(&#39;hello&#39; + world);
<a name="line149"></a> * &lt;/pre&gt;
<a name="line150"></a> *
<a name="line151"></a> * TODO(user): Compile-time checks that this function is only called
<a name="line152"></a> * with compile-time constant expressions.
<a name="line153"></a> *
<a name="line154"></a> * @param {string} s A constant string from which to create a Const.
<a name="line155"></a> * @return {!goog.string.Const} A Const object initialized to stringConst.
<a name="line156"></a> */
<a name="line157"></a>goog.string.Const.from = function(s) {
<a name="line158"></a>  return goog.string.Const.create__googStringSecurityPrivate_(s);
<a name="line159"></a>};
<a name="line160"></a>
<a name="line161"></a>
<a name="line162"></a>/**
<a name="line163"></a> * Type marker for the Const type, used to implement additional run-time
<a name="line164"></a> * type checking.
<a name="line165"></a> * @const
<a name="line166"></a> * @private
<a name="line167"></a> */
<a name="line168"></a>goog.string.Const.TYPE_MARKER_ = {};
<a name="line169"></a>
<a name="line170"></a>
<a name="line171"></a>/**
<a name="line172"></a> * Utility method to create Const instances.
<a name="line173"></a> * @param {string} s The string to initialize the Const object with.
<a name="line174"></a> * @return {!goog.string.Const} The initialized Const object.
<a name="line175"></a> * @private
<a name="line176"></a> */
<a name="line177"></a>goog.string.Const.create__googStringSecurityPrivate_ = function(s) {
<a name="line178"></a>  var stringConst = new goog.string.Const();
<a name="line179"></a>  stringConst.stringConstValueWithSecurityContract__googStringSecurityPrivate_ =
<a name="line180"></a>      s;
<a name="line181"></a>  return stringConst;
<a name="line182"></a>};
</pre>


</body>
</html>
